﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VirtualDrum.componets
{
	public partial class FootHatConfig : Form
	{
		/// <summary>
		/// 构造函数
		/// </summary>
		public FootHatConfig()
		{
			InitializeComponent();
			this.lblFootChatFilePath.Text = frmMain.footHatsMusicPath;
		}

		/// <summary>
		/// 选择音源文件事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnChoiceFootChatMusicFile_Click(object sender, EventArgs e)
		{
			if (DialogResult.OK == this.ofdFootChatFile.ShowDialog())
			{
				this.lblFootChatFilePath.Text = this.ofdFootChatFile.FileName;
				this.btnSure.Focus();
			}
		}

		/// <summary>
		/// 返回主窗体事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnReturn_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		/// <summary>
		/// 确定添加节拍事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnSure_Click(object sender, EventArgs e)
		{
			if (this.tbTotalTime.Text.Equals(""))
			{
				MessageBox.Show("每拍总时间不能为空！");
				return;
			}
			if (this.tbRate.Text.Equals(""))
			{
				MessageBox.Show("节奏率不能为空！");
				return;
			}
			frmMain.footHatsMusicPath = this.lblFootChatFilePath.Text.Trim();
			frmMain.footHatsTimerRate = this.replaceTheRate(this.tbRate.Text.Trim());
			MessageBox.Show("配置成功",frmMain.projectName);
			
		}

		/// <summary>
		/// 换算节拍
		/// </summary>
		/// <param name="rate"></param>
		/// <returns></returns>
		private int replaceTheRate(string rate){
			int divUp = Int32.Parse(rate.Substring(0, rate.IndexOf('/')));
			//int divDown = Int32.Parse(rate.Substring(rate.IndexOf('/') + 1));
			return (int)(this.getTotalTime() / divUp);
		}

		/// <summary>
		/// 得到总时间
		/// </summary>
		/// <returns></returns>
		private double getTotalTime(){
			return  1000 * Double.Parse(tbTotalTime.Text);
		}

		/// <summary>
		/// 清除音源文件路径
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void lblFootChatFilePath_Click(object sender, EventArgs e)
		{
			if (DialogResult.OK == MessageBox.Show("确定清除文件？", frmMain.projectName, MessageBoxButtons.OKCancel))
			{
				this.lblFootChatFilePath.Text = "";
			}
		}
	}
}
